#include "memfd_share.h"
#include<thread>

#define TEST_IMG_FILE "../sources/test1.jpg"

#define IMG_BUF_SIZE  1257924
#define SEND_BUF_SIZE 3 * 1024 * 1024

typedef struct AlgDataHeader
{
    unsigned char dataType;
    unsigned char res[3];
    int dataLen;
} algDataHeader_t;

/*图像数据头*/
typedef struct AlgPictureDataHead
{
    unsigned int dwWidth;    //宽度
    unsigned int dwHeigt;    //高度
    unsigned int dwChan;     //通道号
    unsigned int dwSize;     //图像大小
    unsigned int dwOrder;    //序号
    unsigned int dwExposure; //曝光时间,单位us
    unsigned int dwTimeSec;  //采集时间,s
    unsigned int dwTimeNs;   //采集时间,ns
    char szId[64];
    char szName[32];         //相机名称
    unsigned char byType;    //类型
    unsigned char byFormt;   //格式
    unsigned int devID;     //设备ID
    unsigned char camID;     //相机ID
    unsigned char byRes[12]; //预留
    //unsigned char data[0];
} algPictureDataHead_t;

static int readFile2Buf(unsigned char *pBuf)
{
    FILE *fp = NULL;
    int imgLen = 0;

    fp = fopen(TEST_IMG_FILE, "r");
    if (fp == NULL)
    {
        printf("[%s:%d] open file:%s failed.\n", __func__, __LINE__, TEST_IMG_FILE);
        return -1;
    }
    imgLen = fread(pBuf, sizeof(char), IMG_BUF_SIZE, fp);
    if (imgLen == 0)
    {
        printf("[%s:%d] read file:%s failed.\n", __func__, __LINE__, TEST_IMG_FILE);
        return -1;
    }

    return imgLen;
}

static void handleSubscribeData1()
{
    int iRet = -1;
    iRet = share::MEMFD_Init();
    char topic[256] = "hello";

    algDataHeader_t stAlgDataHeader;
    algPictureDataHead_t stImgHeader;
    unsigned char *imgBuf = NULL;
    unsigned char *sendBuf = NULL;
    unsigned char dataHeader[1024];
    unsigned char *pHeader = NULL;
    int imgLen = 0;
    unsigned int headerLen = 0;
    timespec timeStamp;

    imgBuf = new (std::nothrow) unsigned char[IMG_BUF_SIZE];
    if (imgBuf == NULL)
    {
        printf("[%s:%d]  new imgBuf failed.\n", __func__, __LINE__);
        return;
    }
    sendBuf = new (std::nothrow) unsigned char[SEND_BUF_SIZE];
    if (sendBuf == NULL)
    {
        printf("[%s:%d]  new sendBuf failed.\n", __func__, __LINE__);
        return;
    }
    memset(imgBuf, 0, sizeof(imgBuf));

    imgLen = readFile2Buf(imgBuf);
    if (imgLen == 0)
    {
        return;
    }
    stAlgDataHeader.dataType = 1;
    stAlgDataHeader.dataLen = sizeof(algPictureDataHead_t) + imgLen;

    /*发布算法分析结果数据*/
    while (1)
    {
        memset(sendBuf, 0, SEND_BUF_SIZE);
        pHeader = sendBuf;
        memcpy(pHeader, &stAlgDataHeader, sizeof(stAlgDataHeader));
        pHeader += sizeof(stAlgDataHeader);

        stImgHeader.camID = 1;
        stImgHeader.devID = 111111;
        stImgHeader.dwHeigt = 2048;
        stImgHeader.dwWidth = 2448;
        clock_gettime(CLOCK_REALTIME, &timeStamp);
        stImgHeader.dwTimeSec = timeStamp.tv_sec;
        stImgHeader.dwTimeNs = timeStamp.tv_nsec;
        stImgHeader.dwChan = 3;
        stImgHeader.dwSize = imgLen;

        memcpy(pHeader, &stImgHeader, sizeof(stImgHeader));
        pHeader += sizeof(stImgHeader);
        headerLen = sizeof(stAlgDataHeader) + sizeof(stImgHeader);
        printf("[%s:%d] publisher data  len:%d, devID is %d ,timestamp is %d.%d\n", __func__, __LINE__, headerLen + imgLen,  stImgHeader.devID,stImgHeader.dwTimeSec, stImgHeader.dwTimeNs);

        // iRet = share::MEMFD_Adv_Parameter_Set(topic, IMG_BUF_SIZE * 5, IMG_BUF_SIZE);
        // if (iRet != 0)
        // {
        //     printf("[%s:%d] set publisher adv parameters failed.\n", __func__, __LINE__);
        //     return;
        // }

        iRet = share::MEMFD_Publisher2(topic, sendBuf, headerLen, imgBuf, imgLen);
        if (iRet != 0)
        {
            // printf("[%s:%d] publisher failed.\n", __func__, __LINE__);
            usleep(100 * 1000);
            continue;
        }

        usleep(100 * 1000);
    }

    if (imgBuf)
    {
        delete (imgBuf);
    }
    if (sendBuf)
    {
        delete (sendBuf);
    }

    share::MEMFD_Release();
    return;
}


int main(int argc, const char **argv)
{

    std::thread *subDataThread1;

    subDataThread1 = new std::thread(handleSubscribeData1);
    subDataThread1->detach();

    while(1)
    {
        sleep(1);
    }
    return 0;
}